﻿namespace Crux.Core.SQL.Formatter.Core.Util;
/// <summary>
/// 
/// </summary>
public class Utils
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="list"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static List<T> NullToEmpty<T>(List<T> list)
    {
        return list ?? new List<T>();
    }
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="R"></typeparam>
    public static R? FirstNotnull<R>(params Func<R>[] suppliers) where R : class
    {
        return suppliers.FirstOrDefault(supplier => supplier() is not null)?.Invoke();
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="s"></param>
    /// <param name="n"></param>
    /// <returns></returns>
    public static string Repeat(string s, int n)
    {
        return string.Concat(Enumerable.Repeat(s, n));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="l1"></param>
    /// <param name="l2"></param>
    /// <returns></returns>
    public static List<T> Concat<T>(List<T> l1, List<T> l2)
    {
        return l1.Concat(l2).ToList();
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="strings"></param>
    /// <returns></returns>
    public static JSLikeList<string> SortByLengthDesc(JSLikeList<string> strings)
    {
        return new JSLikeList<string>(strings.ToList().OrderByDescending(s => s.Length).ToList());
    }
}